package com.test.prolenders.view_prolenders.process_widget_prolenders;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import static com.test.prolenders.config_prolenders.ProcessConfig_prolenders.Data_Action_Contact_Phone;

import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.test.prolenders.R;
import com.test.prolenders.base_prolenders.BaseProcessWidgetManager_prolenders;
import com.test.prolenders.bean_prolenders.process_prolenders.ProcessInfo_prolenders;
import com.test.prolenders.bean_prolenders.process_prolenders.ProcessInfo_Data_prolenders;
import com.test.prolenders.config_prolenders.CountryConfig_prolenders;
import com.test.prolenders.util_prolenders.UserInfoUtil_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;

public class InputViewManager_prolenders extends BaseProcessWidgetManager_prolenders {

    private TextView titleTv;
    private EditText inputEt;

    public InputViewManager_prolenders(View view, ProcessInfo_Data_prolenders data, String page){
        super(view,data,page);
    }

    @Override
    protected void init(){

        titleTv = mView.findViewById(R.id.vpi_tv_title);
        inputEt = mView.findViewById(R.id.vpi_et_input);

        setTitle(mData.getTitle());
        inputEt.setHint(mData.getHint());

        //联系人电话输入数字
        if (mData.getAction().equalsIgnoreCase(Data_Action_Contact_Phone)) inputEt.setInputType(InputType.TYPE_CLASS_NUMBER);

        //加纳卡号和卡名填充
        if (CountryConfig_prolenders.getInstance().isGhana() && mData.getParamName().equalsIgnoreCase("walletMobile")){
            inputEt.setText(UserInfoUtil_prolenders.getInstance().getMobile());
        }
        if (CountryConfig_prolenders.getInstance().isGhana() && mData.getParamName().equalsIgnoreCase("walletUsername")){
            inputEt.setText(ProcessInfo_prolenders.getInstance().getRecipientName());
        }

        inputEt.setOnFocusChangeListener((v, hasFocus) -> HttpUtil_prolenders.getInstance().saveBuriedPoint(page,mData.getParamName().toUpperCase()+"_INPUT",hasFocus ? "":getInput()));

    }

    @Override
    public boolean checkInput() {
        if (!mData.isMustInput()) return true;
        return !TextUtils.isEmpty(inputEt.getText());
    }

    @Override
    public String getInput() {
        return inputEt.getText().toString().trim();
    }

    public void addTextChangeListener(TextWatcher textWatcher){
        inputEt.addTextChangedListener(textWatcher);
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv.setVisibility(GONE);
        else {
            titleTv.setVisibility(VISIBLE);
            titleTv.setText(title);
        }
    }

}
